package com.hschinese.pronunciation.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.fragments.ResultPinyinFragment;
import com.hschinese.pronunciation.fragments.ToneResultFragment;
import com.hschinese.pronunciation.widget.Header;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ToneCheckReportActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.iv_report_back)
    ImageView ivReportBack;
    @Bind(R.id.report_pinyin_btn)
    ImageView reportPinyinBtn;
    private Header mHeader = null;
    private ImageView mToneBtn = null, mPinyinBtn;
    //    private FrameLayout mFragmentContainer;
    private FragmentManager mFragmentManager;
    private ResultPinyinFragment mPinyinResultFragment;
    private ToneResultFragment mToneResultFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tone_report_result);
        ButterKnife.bind(this);
        mFragmentManager = getSupportFragmentManager();
        initViews();
        initData();
    }

    private void initViews() {

        mToneBtn = (ImageView) findViewById(R.id.report_tone_btn);
        mPinyinBtn = (ImageView) findViewById(R.id.report_pinyin_btn);

        mToneBtn.setOnClickListener(this);
        mPinyinBtn.setOnClickListener(this);
        ivReportBack.setOnClickListener(this);

//        mFragmentContainer = (FrameLayout)findViewById(R.id.report_fragment_container);
    }

    private void initData() {
        mPinyinResultFragment = new ResultPinyinFragment();
        mToneResultFragment = new ToneResultFragment(ToneCheckReportActivity.this);
        String lessonType = getIntent().getStringExtra("lessonType");
        if(lessonType.equals("8") || lessonType.equals("9")){
            setFragment(1);
        }else {
            setFragment(0);
        }

    }

    private void setFragment(int i) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (i == 0) {
            transaction.replace(R.id.report_fragment_container, mToneResultFragment);
            mToneBtn.setImageResource(R.mipmap.shengdiao_click);
            mPinyinBtn.setImageResource(R.mipmap.pinyin_unclick);

        } else {
            transaction.replace(R.id.report_fragment_container, mPinyinResultFragment);
            mPinyinBtn.setImageResource(R.mipmap.pinyin_click);
            mToneBtn.setImageResource(R.mipmap.shengdiao_unclick);

        }
        transaction.commit();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.report_tone_btn) {
            setFragment(0);
        } else if (id == R.id.report_pinyin_btn) {
            setFragment(1);
        }else if (id == R.id.iv_report_back) {
            this.finish();
        }
    }
}
